package com.sky.anotion.validator;

import com.sky.anotion.ValidSnowflakeId;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.List;
public class ValidSnowflakeIdValidator implements ConstraintValidator<ValidSnowflakeId, Object> {

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (value == null) return true;
        if (value instanceof String) {
            return isValidId((String) value);
        }
        if (value instanceof List<?>) {
            List<?> list = (List<?>) value;
            for (Object item : list) {
                if (!(item instanceof String) || !isValidId((String) item)) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }
    private boolean isValidId(String id) {
        return id != null && id.matches("\\d{15,20}");
    }
}
